package com.me.modules.demo.controller;

import com.me.modules.common.page.Page;
import com.me.modules.common.utils.Result;
import com.me.modules.common.utils.StringUtils;
import com.me.modules.common.utils.Utils;
import com.me.modules.demo.entity.HiLeaveEntity;
import com.me.modules.demo.service.HiLeaveService;
import com.me.modules.sys.entity.FieldDictionaryEntity;
import com.me.modules.sys.entity.ValueDictionaryEntity;
import com.me.modules.sys.service.FieldDictionaryService;
import com.me.modules.sys.service.ValueDictionaryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 类的功能描述.
 * 业务树
 * @Auther me
 * @Date 2018/7/25
 */
@RequestMapping("demo/hileave")
@Controller
public class HiLeaveController {

    @Autowired
    HiLeaveService leaveService;
    @Autowired
    FieldDictionaryService feldDictionaryService;
    @Autowired
    ValueDictionaryService valueDictionaryService;

    /**
     * 请假列表
     * @param model
     * @param leaveEntity
     * @param request
     * @return
     */
    @RequestMapping("list")
    @RequiresPermissions("act:model:all")
    public String list(Model model , HiLeaveEntity leaveEntity, HttpServletRequest request){
        int pageNum = Utils.parseInt(request.getParameter("pageNum"), 1);
        Page<HiLeaveEntity> page = leaveService.findPage(leaveEntity, pageNum);
        List<HiLeaveEntity>  leaveList = page.getResult();
        List<HiLeaveEntity>  newleaveList = new ArrayList<HiLeaveEntity>();
        List list =new ArrayList();
        List<FieldDictionaryEntity> fieldList=feldDictionaryService.queryByTableName("leave");
        if(fieldList.size()>0){

            if( leaveList.size()>0){
                for(int i=0;i<leaveList.size();i++){
                    HiLeaveEntity leave =leaveList.get(i);
                    Long tableId=leaveList.get(i).getId();

                    List<ValueDictionaryEntity> valueList=new ArrayList<ValueDictionaryEntity>();
                    for(int j=0;j<fieldList.size();j++){
                        Long fieldID=fieldList.get(j).getId();

                        ValueDictionaryEntity value=valueDictionaryService.findByFieldIdAndUserId(fieldID,tableId.toString());
                        if(value!=null){
                            valueList.add(value);
                        }else{
                            valueList.add(new ValueDictionaryEntity());
                        }
                    }
                    leave.setValueDictionarys(valueList);
                    newleaveList.add(leave);
                }
            }
            model.addAttribute("page",new Page(newleaveList,page.getTotal(),page.getPageSize() , page.getPageNum()));

        }else{
            model.addAttribute("page",page);

        }
        model.addAttribute("page",new Page(newleaveList,page.getTotal(),page.getPageSize() , page.getPageNum()));
        model.addAttribute("leave",leaveEntity);
        model.addAttribute("fieldList",fieldList);
        return "demo/hileave";
    }

    /**
     * 请假详情
     * @param model
     * @param id
     * @param request
     * @return
     */
    @RequestMapping("info")
    @RequiresPermissions("act:model:all")
    public String info(Model model , String id, HttpServletRequest request){
        List<FieldDictionaryEntity> fieldList=feldDictionaryService.queryByTableName("leave");
        if(!StringUtils.isEmpty(id)){
            HiLeaveEntity leaveEntity = leaveService.queryObject(id);
            if(fieldList.size()>0){
                Long tableId=leaveEntity.getId();
                List<ValueDictionaryEntity> valueList=new ArrayList<ValueDictionaryEntity>();
                for(int j=0;j<fieldList.size();j++){
                    Long fieldID=fieldList.get(j).getId();

                    ValueDictionaryEntity value=valueDictionaryService.findByFieldIdAndUserId(fieldID,tableId.toString());
                    if(value!=null){
                        valueList.add(value);
                    }else{
                        valueList.add(new ValueDictionaryEntity());
                    }
                }
                leaveEntity.setValueDictionarys(valueList);
            }

            model.addAttribute("leave",leaveEntity);
        }
        model.addAttribute("fieldList",fieldList);
        return "demo/leaveEdit";
    }

    /**
     * 编辑
     * @param leaveEntity
     * @return
     */
    @RequestMapping(value = "edit",method = RequestMethod.POST)
    @RequiresPermissions("act:model:all")
    @ResponseBody
    public Result edit(HiLeaveEntity leaveEntity){
        if(StringUtils.isEmpty(leaveEntity.getId())){
            leaveService.save(leaveEntity);
            if(leaveEntity.getValueDictionarys().size()>0) {
                for (ValueDictionaryEntity value : leaveEntity.getValueDictionarys()) {
                        if(!StringUtils.isEmpty(value.getFieldValue())){
                            value.setTableId(leaveEntity.getId().toString());
                            valueDictionaryService.save(value);
                        }
                }
            }
        }else {
            leaveService.update(leaveEntity);
            if(leaveEntity.getValueDictionarys().size()>0) {
                for (ValueDictionaryEntity value : leaveEntity.getValueDictionarys()) {
                    ValueDictionaryEntity valueDictionaryEntity=valueDictionaryService.findByFieldIdAndUserId(value.getFieldId(),leaveEntity.getId().toString());
                    if(valueDictionaryEntity!=null){
                        valueDictionaryService.delete(valueDictionaryEntity.getId());
                    }
                    if(!StringUtils.isEmpty(value.getFieldValue())){
                        value.setTableId(leaveEntity.getId().toString());
                        valueDictionaryService.save(value);
                    }


                }
            }
        }
        return Result.ok();
    }


    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping(value = "delete",method = RequestMethod.POST)
    @RequiresPermissions("act:model:all")
    @ResponseBody
    public Result edit(String id){
       if(leaveService.delete(id)<1){
           return Result.error("删除请假条失败");
       }
        return Result.ok("删除请假条成功");
    }








}
